//
//  KDDUpdate.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "KDDUpdate.h"

@implementation KDDUpdate
KNSCopyingImpl

- (instancetype)initWithPosition:(KDDPosition *)position updateType:(KDDUpdateType)updateType
{
    if (self = [self initWithTarget:(KDDTargetType)position.viewType updateType:updateType])
    {
        _section = position.section;
        [self addPosition:position];
    }
    return self;
}

- (instancetype)initWithSectionData:(KDDSectionData *)sectionData updateType:(KDDUpdateType)updateType
{
    if (self = [self initWithTarget:KDDTargetSection updateType:updateType])
    {
        [self addSectionData:sectionData];
    }
    return self;
}

- (instancetype)initWithTarget:(KDDTargetType)target updateType:(KDDUpdateType)updateType
{
    if (self = [super init])
    {
        _target = target;
        _updateType = updateType;
    }
    return self;
}

- (NSMutableArray *)modelList
{
    if (!_modelList)
    {
        _modelList = [NSMutableArray array];
    }
    return _modelList;
}

- (NSMutableIndexSet *)indexSet
{
    if (!_indexSet)
    {
        _indexSet = [NSMutableIndexSet indexSet];
    }
    return _indexSet;
}

- (BOOL)matchPosition:(KDDPosition *)position
{
    return (KDDTargetType)position.viewType == self.target && position.section == self.section;
}

- (void)addPosition:(KDDPosition *)position
{
    if (position.model != nil)
    {
        if (self.target == KDDViewTypeCell)
        {
            [self.modelList addObject:position.model];
        }
        else
        {
            self.model = position.model;
        }
    }
    if (self.target == KDDViewTypeCell)
    {
        [self.indexSet addIndex:position.index];
    }
}

- (void)addSectionData:(KDDSectionData *)sectionData
{
    [self.modelList addObject:sectionData];
    [self.indexSet addIndex:sectionData.index];
}

@end

@implementation KOSUpdate
KNSCopyingImpl

- (instancetype)initWithTarget:(KDDTargetType)target updateType:(KDDUpdateType)updateType
{
    if (self = [super init])
    {
        self.updateType = updateType;
        self.target = target;
        self.updateList = [NSMutableArray array];
        self.positionList = [NSMutableArray array];
    }
    return self;
}

@end
